<?php

namespace Common\Logic;

/**
 * Description of AdminAuthorityLogic 权限控制
 * 
 * @author songanwei
 * @date 2016-1-5
 */
class AdminAuthorityLogic extends \Common\Controller\BaseController {
    /**
     * 个人权限信息数组
     * @var array/null
     */
    private $adminAuthority = null;
    
    private $authorityOpposite = array(AUTHORITY_CONTACT_INFO_BLOCK);

    /**
     * 检查用户操作权限
     * @modify trainyao 2016-06-13 增加没具体接口的操作权限权限校验
     * @param integer $hotelId  客栈ID
     * @param integer $userId   操作人ID
     */
    public function check($hotelId, $userId = null, $hotelAuthorityId = null) {
        if (is_null($userId)) {
            $userId = getUserId();
        }
        if (empty($userId)) {
            returnJson(403, 202, 'error');
            return false;
        }

        //检查是否为客栈管理员
        $adminHotelModel = D('AdminHotel');
        $adminHotel = $adminHotelModel->getInfoByAdminHotel($userId, $hotelId);
        if (!$adminHotel) {
            returnJson(403, 202, 'error');
            return false;
        }

        //不是客栈创建人员，需要检查操作权限
        if ($adminHotel['role'] != HOTEL_ROLE_OWNER) {
            if (is_null($hotelAuthorityId)) {
                //获取
                $targetValue = implode("/", array(MODULE_NAME, CONTROLLER_NAME, ACTION_NAME));
                $authorityAccessModel = D('AuthorityAccess');
                $authorityAccess = $authorityAccessModel->getInfoByAction($targetValue);
                if (!$authorityAccess) {
                    returnJson(403, 202, 'error');
                    return false;
                }
                $authorityId = $authorityAccess['hotelauthority_id'];
                //查询当前用户权限
                $adminAuthorityModel = D('AdminAuthority');
                $adminAuthority = $adminAuthorityModel->getInfoByAdminHotelAuthority($userId, $hotelId, $authorityId);
                if (!$adminAuthority) {
                    returnJson(403, 202, 'error');
                    return false;
                }
            } else {
                if (empty($this->adminAuthority)) {
                    $this->adminAuthority = $this->show($hotelId, $userId);
                }
                if (in_array($hotelAuthorityId, $this->authorityOpposite)) {
                    return !in_array($hotelAuthorityId, $this->adminAuthority['right']);
                }
                return in_array($hotelAuthorityId, $this->adminAuthority['right']);
            }
        }
        return true;
    }

    /**
     * 返回用户的控制权限
     * @modify songanwei 2016-01-27
     * @modify trainyao 2016-06-13 增加没具体接口的操作权限数据添加
     * @param integer $hotelId  客栈ID
     * @param integer $userId  用户ID
     * @return type
     */
    public function show($hotelId, $userId) {
        //检查客栈是否为管理员
        $adminHotelModel = D('AdminHotel');
        $adminHotel = $adminHotelModel->getInfoByAdminHotel($userId, $hotelId);
        if ($adminHotel['role'] == HOTEL_ROLE_OWNER) {
            return array('role' => 1, 'access' => array());
        }
        //非客栈创建者
        $adminAuthorityModel = D('AdminAuthority');
        $adminAuthoritys = $adminAuthorityModel->getInfoByAdminHotelAuthority($userId, $hotelId);

        $hotelAuthorityIdArray = array_column($adminAuthoritys, "hotelauthority_id");
        //查询能操作的权限方法
        $authorityAccessModel = D('AuthorityAccess');
        $authorityAccess = $authorityAccessModel->getAccessByAuthorityId($hotelAuthorityIdArray);

        // 没具体接口的操作权限数据添加
        $activeAuthorityId = array_unique(array_column($authorityAccess, 'hotelauthority_id'));

        return array(
            'role' => 2,
            'access' => array_column($authorityAccess, "action"),
            'right' => array_values(array_diff($hotelAuthorityIdArray, $activeAuthorityId)),
        );
    }

    /**
     * 检查用户是否有超级管理员权限
     * @modify trainyao 2016-06-06 创建
     * @modify trainyao 2016-07-28 修改为可以校验多个客栈操作权限
	 * @modify zhangzhen 2016-12-27   修改掌柜添加为掌柜做子账号的权限问题
     * @param  integer   $userId    用户ID
     * @return boolean              是否有
     */
    public function checkIsAdmin($userId, $hotelId = null) {
        if (!empty($this->adminAuthority) && isset($this->adminAuthority['role'])) {
            return $this->adminAuthority['role'] == HOTEL_ROLE_OWNER;
        }
        
        $adminHotelModel = D('AdminHotel');
        $adminHotelInfo = $adminHotelModel->getInfosByAdmin($userId);
        if (empty($adminHotelInfo)) {
            return false;
        }
        if (empty($hotelId)) {
            return true;
        }

        // 根据客栈校验是否超级管理员
        if (!is_array($hotelId)) {
            $hotelId = explode(',', $hotelId);
        }
        $adminHotelInfo = getArrayValueAsIndex($adminHotelInfo, 'hotelentity_id');
		//掌柜添加掌柜做子管
		return empty(array_diff($hotelId, array_keys($adminHotelInfo)));
    }
    /**
     * 检查用户是否有超级管理员权限
     * @modify zhangzhen 2016-12-27   修改掌柜添加为掌柜做子账号的权限问题
     * @param  integer   $userId    用户ID
     * @return boolean              是否有
     */
    public function canSetStatus($userId, $hotelId = null) {
        if (!empty($this->adminAuthority) && isset($this->adminAuthority['role'])) {
            return $this->adminAuthority['role'] == HOTEL_ROLE_OWNER;
        }
        
        $adminHotelModel = D('AdminHotel');
        $adminHotelInfo = $adminHotelModel->getInfosByAdmin($userId);
        if (empty($adminHotelInfo)) {
            return false;
        }
        if (empty($hotelId)) {
            return true;
        }

        // 根据客栈校验是否超级管理员
        if (!is_array($hotelId)) {
            $hotelId = explode(',', $hotelId);
        }
        $adminHotelInfo = getArrayValueAsIndex($adminHotelInfo, 'hotelentity_id');
        //掌柜添加掌柜做子管
        //return empty(array_diff($hotelId, array_keys($adminHotelInfo)));
        return array_diff($hotelId, array_keys($adminHotelInfo));
    }
}
